package com.googlecode.tidej.client.project;

import java.io.IOException;

import org.vectomatic.file.File;
import org.vectomatic.file.FileUploadExt;

import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.RadioButton;
import com.google.gwt.user.client.ui.TextBox;
import com.googlecode.tidej.client.Dialog;
import com.googlecode.tidej.client.TidejController;
import com.googlecode.tidej.client.PersistentState;
import com.googlecode.tidej.client.util.DataSource;
import com.googlecode.tidej.client.util.FileDataSource;
import com.googlecode.tidej.client.util.HttpDataSource;

public class NewProjectDialog extends Dialog {
  TextBox nameTextBox = new TextBox();

  RadioButton fromEmptyButton = new RadioButton("source", "Empty");
  RadioButton fromExampleButton = new RadioButton("source", "From Example");
  RadioButton fromUrlButton = new RadioButton("source", "From URL");
  RadioButton fromFileButton = new RadioButton("source", "From file");
  
  TextBox urlTextBox = new TextBox();
  
  TidejController controller;
  PersistentState projects;
  ListBox sampleListBox = new ListBox();
  FlowPanel mainPanel = new FlowPanel();
  FileUploadExt fileWidget = new FileUploadExt(false);

  public NewProjectDialog(final TidejController controller) {
    super("New Project");
    this.controller = controller;
    FlexTable table = new FlexTable();
//    setText("Projects");
    
    table.setText(0, 0, "New project name");
    table.setWidget(0, 1, nameTextBox);

    // Empty new project
    table.setWidget(1, 0, fromEmptyButton);
    fromEmptyButton.setValue(true);

    // Initialized from an example
    table.setWidget(2, 0, fromExampleButton);
    sampleListBox.addItem("");
    sampleListBox.addItem("http://tidej.net/samples/helloworld.html");
    sampleListBox.addItem("http://tidej.net/samples/canvasoids.html");
    sampleListBox.addItem("http://tidej.net/samples/canvasout.html");
    table.setWidget(2, 1, sampleListBox);
    sampleListBox.addChangeHandler(new ChangeHandler() {
      @Override
      public void onChange(ChangeEvent event) {
        if (sampleListBox.getSelectedIndex() != 0) {
          fromExampleButton.setValue(true);
        }
      }
    });

    // Initialized from a file
    table.setWidget(3, 0, fromFileButton);
    table.setWidget(3, 1, fileWidget);
    fileWidget.addChangeHandler(new ChangeHandler() {    
      @Override
      public void onChange(ChangeEvent event) {
        fromFileButton.setValue(true);
      }
    });
    
    table.setWidget(4, 0, fromUrlButton);
    table.setWidget(4, 1, urlTextBox);
    urlTextBox.addChangeHandler(new ChangeHandler() {
      @Override
      public void onChange(ChangeEvent event) {
        fromUrlButton.setValue(true);
      }
    });
    
    getContentPanel().add(table);
    
    Button createButton = new Button("Create");
    createButton.addClickHandler(new ClickHandler() {
      @Override
      public void onClick(ClickEvent event) {
        create(nameTextBox.getText());
      }
    });
    addButton(createButton);
  }
  
  public void run() {
    projects = PersistentState.load();
    show();
    center();
  }
  
  void create(String name) {
    name = name.trim();
    if (name.length() == 0) {
      Window.alert("Project name must not be empty.");
      return;
    }
    
    if (ProjectManager.exists(name)) {
      Window.alert("Project '" + name + "' exists already.");
      return;
    }

    DataSource fromSource;
    if (Boolean.TRUE.equals(fromExampleButton.getValue())) {
      String sampleUrl = 
          sampleListBox.getItemText(sampleListBox.getSelectedIndex());
      fromSource = new HttpDataSource(sampleUrl);
    } else if (Boolean.TRUE.equals(fromFileButton.getValue())) {
      if (fileWidget.getFiles() == null || fileWidget.getFiles().getLength() > 0) {
        Window.alert("Please select a file.");
        return;
      }
      File file = fileWidget.getFiles().getItem(0);
      fromSource = new FileDataSource(file);
    } else if (Boolean.TRUE.equals(fromUrlButton.getValue())) {
      String url = urlTextBox.getValue();
      if (url.isEmpty()) {
        Window.alert("Please enter a URL.");
      }
      HttpDataSource httpSource = new HttpDataSource(url);
      fromSource = httpSource;
    } else {
      fromSource = null;
    }

    ProjectMetaData entry = ProjectMetaData.create(name);
    try {
      projects.setLastOpened(name);
      projects.save();
    } catch (IOException e) {
      Window.alert("Saving projects failed: " + e.getMessage());
      return;
    }
    controller.createProject(entry, fromSource);
    hide();
  }
}
